from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from webdriver_manager.chrome import ChromeDriverManager

# 初始化浏览器驱动
driver = webdriver.Chrome(ChromeDriverManager().install())
# 打开百度首页
driver.get("https://www.baidu.com/")
# 页面最大化
driver.maximize_window()
time.sleep(3)

# 获取句柄
print(driver.window_handles)
# # 输入坚果
# driver.find_element(By.ID, "kw").send_keys("坚果")
# time.sleep(2)
#
# # 点击-百度一下
# driver.find_element(By.ID, "su").click()
# time.sleep(3)
#
# # 点击-百度百科
# driver.find_element(By.XPATH, "//a[text()=' - 百度百科']").click()
# time.sleep(5)
#
# print(driver.window_handles)
# # 通过句柄切换页签
# driver.switch_to.window(driver.window_handles[-1])
# # 点击-首页
# driver.find_element(By.XPATH, "//a[text()='首页']").click()
# time.sleep(10)

# 定位一组元素
elements = driver.find_elements(By.XPATH, "//a[starts-with(@class, 'title-content')]")
# print(elements)
for ele in elements:
    ele.click()
    print(driver.window_handles)
    time.sleep(3)

driver.quit()